package org.dromara.business.service.impl;

import cn.hutool.core.collection.CollUtil;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.domain.BaseEntity;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.system.domain.SysUser;
import org.dromara.system.mapper.SysUserMapper;
import org.springframework.stereotype.Service;
import org.dromara.business.domain.bo.UserLevelExperienceLogBo;
import org.dromara.business.domain.vo.UserLevelExperienceLogVo;
import org.dromara.business.domain.UserLevelExperienceLog;
import org.dromara.business.mapper.UserLevelExperienceLogMapper;
import org.dromara.business.service.IUserLevelExperienceLogService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 用户等级经验变动记录Service业务层处理
 *
 * @author Hao Tong Xue
 * @date 2025-04-03
 */
@RequiredArgsConstructor
@Service
public class UserLevelExperienceLogServiceImpl implements IUserLevelExperienceLogService {

    private final UserLevelExperienceLogMapper baseMapper;

    /**
     * 查询用户等级经验变动记录
     *
     * @param id 主键
     * @return 用户等级经验变动记录
     */
    @Override
    public UserLevelExperienceLogVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询用户等级经验变动记录列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 用户等级经验变动记录分页列表
     */
    @Override
    public TableDataInfo<UserLevelExperienceLogVo> queryPageList(UserLevelExperienceLogBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<UserLevelExperienceLog> lqw = buildQueryWrapper(bo);
        Page<UserLevelExperienceLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的用户等级经验变动记录列表
     *
     * @param bo 查询条件
     * @return 用户等级经验变动记录列表
     */
    @Override
    public List<UserLevelExperienceLogVo> queryList(UserLevelExperienceLogBo bo) {
        LambdaQueryWrapper<UserLevelExperienceLog> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<UserLevelExperienceLog> buildQueryWrapper(UserLevelExperienceLogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<UserLevelExperienceLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(UserLevelExperienceLog::getUserId, bo.getUserId() != null ? bo.getUserId() : LoginHelper.getUserId());
        lqw.eq(bo.getChangeExperience() != null, UserLevelExperienceLog::getChangeExperience, bo.getChangeExperience());
        lqw.eq(bo.getChangeType() != null, UserLevelExperienceLog::getChangeType, bo.getChangeType());
        lqw.orderByDesc(BaseEntity::getCreateTime);
        return lqw;
    }

    /**
     * 新增用户等级经验变动记录
     *
     * @param bo 用户等级经验变动记录
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(UserLevelExperienceLogBo bo) {
        UserLevelExperienceLog add = MapstructUtils.convert(bo, UserLevelExperienceLog.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户等级经验变动记录
     *
     * @param bo 用户等级经验变动记录
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(UserLevelExperienceLogBo bo) {
        UserLevelExperienceLog update = MapstructUtils.convert(bo, UserLevelExperienceLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(UserLevelExperienceLog entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除用户等级经验变动记录信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
